package edu.ts.crowdfunding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.ts.crowdfunding.constant.JwtClaimsConstant;
import edu.ts.crowdfunding.domain.User;
import edu.ts.crowdfunding.properties.JwtProperties;
import edu.ts.crowdfunding.service.UserService;
import edu.ts.crowdfunding.mapper.UserMapper;
import edu.ts.crowdfunding.utils.JwtUtil;
import edu.ts.crowdfunding.utils.Result;
import edu.ts.crowdfunding.vo.UserLoginVo;
import edu.ts.crowdfunding.vo.UserRegisterVo;
import edu.ts.crowdfunding.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Xiaotong Fan
* @description 针对表【sys_user(System Admin User Table)】的数据库操作Service实现
* @createDate 2025-06-25 14:53:46
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper  userMapper;

    @Autowired
    private JwtProperties jwtProperties;


    @Override
    public Result login(UserVo userVo) {
        //校验
        String username = userVo.getUserUsername();
        String userpwd = userVo.getUserPassword();
        if (null == username || null == userpwd || username.isEmpty()|| userpwd.isEmpty()) {
            return new Result<String>(0,"用户名或密码为空", "");
        }
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserUsername, username).eq(User::getUserPassword, userpwd));
        //判断状态
        if (user == null){
            return new Result<String>(0,"用户名或密码错误", "");
        }
        if (user.getUserStatus() !=1){
            return new Result<String>(0,"账号状态异常", "");
        }
        //生成令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getUserId());
        String token = JwtUtil.creatJWT(jwtProperties.getSecretKey(), jwtProperties.getTtl(), claims);

        UserLoginVo userLoginVo = UserLoginVo.builder()
                .userId(user.getUserId())
                .userUsername(user.getUserUsername())
                .userPassword(user.getUserPassword())
                .token(token)
                .build();
        return new Result<String>(1,"登录成功", userLoginVo.toString());

    }

    @Override
    public Result listUsers() {
        try {
            List<User> list = userMapper.selectList(null);
            return new Result(1, "查询成功",list);
        } catch (Exception e) {
            return new Result(0, "查询失败:"+e.getMessage(), "");
        }
    }

    @Override
    public Result register(UserRegisterVo user) {
        String userUsername = user.getUserUsername();
        String password = user.getUserPassword();
        //校验两次密码
        if (!user.getTwicePassword().equals(password)){
            return new Result(0, "两次密码不一致", "");
        }
        //TODO校验验证码
        User is_exited = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserUsername, userUsername));
        if (is_exited != null){
            return new Result(0, "用户名已存在", "");
        }
        User userPO = new User();
        userPO.setUserUsername(userUsername);
        userPO.setUserPassword(password);
        userPO.setUserNickname(user.getNickName());

        int count = userMapper.insert(userPO);
        if (count == 0){
            return new Result<String>(0,"注册失败","");
        }else {
            User tempUser = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserUsername, userUsername).eq(User::getUserPassword, password));
            //生成令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.USER_ID, tempUser.getUserId());
            String token = JwtUtil.creatJWT(jwtProperties.getSecretKey(), jwtProperties.getTtl(), claims);

            UserLoginVo userLoginVo = UserLoginVo.builder()
                    .userId(tempUser.getUserId())
                    .userUsername(user.getUserUsername())
                    .userPassword(user.getUserPassword())
                    .token(token)
                    .build();
            return new Result<String>(1,"注册成功","");
        }
    }
}




